using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class UI_StatSlot : UI_ItemSlot
{
    [SerializeField] private StatType type;
    [SerializeField] private TextMeshProUGUI statName;
    [SerializeField] private TextMeshProUGUI statValue;
    private Player_Stats playerStat; 

    private void OnValidate()
    {
        gameObject.name = "UI_Stat - " + type.ToString();
        statName.text = GetStatNameByType(type);
        uI = GetComponentInParent<UI>();
    }

    private void Awake()
    {
        playerStat = FindAnyObjectByType<Player_Stats>();
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        uI.statToolTip.ShowToolTip(true, rect, type);
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        uI.statToolTip.ShowToolTip(false, rect, type);
    }
    public void UpdateStatSlotValue()
    {
        Stat statToUpdate = playerStat.GetStatByType(type); 
        if (statToUpdate == null && type != StatType.ElementalDamage)
            return;
        float value = GetStatValue();
        statValue.text = IspercentageStat(type) ? value + "%" : value.ToString(); 
    }
    private string GetStatNameByType(StatType type)
    {
        return type switch
        {
            StatType.MaxHealth => "Max Health",
            StatType.HealthRegen => "Health Regeneration",
            StatType.Strength => "Strength",
            StatType.Agility => "Agility",
            StatType.Intelligence => "Intelligence",
            StatType.Vitality => "Vitality",
            StatType.AttackSpeed => "Attack Speed",
            StatType.Damage => "Damage",
            StatType.CritChance => "Critcal Chance",
            StatType.CritPower => "Critcal Power",
            StatType.ArmorReduction => "Armor Reduction",
            StatType.FireDamage => "Fire Damage",
            StatType.IceDamage => "Ice Damage",
            StatType.LightningDamage => "Lightning Damage",
            StatType.ElementalDamage => "Elemental Damage",
            StatType.Armor => "Armor",
            StatType.Evasion => "Evasion",
            StatType.IceResistance => "Ice Resistance",
            StatType.FireResistance => "Fire Resistance",
            StatType.LightningResistance => "Lightning Resistance",
            _ => "UnKnown Stat",
        };
    }

    private float GetStatValue()
    {

        return type switch
        {
            //Major Stat
            StatType.Strength => playerStat.major.strength.GetValue(),
            StatType.Agility => playerStat.major.agility.GetValue(),
            StatType.Intelligence => playerStat.major.intelligence.GetValue(),
            StatType.Vitality => playerStat.major.vitality.GetValue(),

            //Defense Stat
            StatType.Armor => playerStat.defense.armor.GetValue(),
            StatType.Evasion => playerStat.defense.evasion.GetValue(),
            StatType.FireResistance => playerStat.GetElementalResistance(ElementType.Fire) * 100,
            StatType.IceResistance => playerStat.GetElementalResistance(ElementType.Ice) * 100,
            StatType.LightningResistance => playerStat.GetElementalResistance(ElementType.Lightning) * 100,


            //Offensive Stat
            StatType.Damage => playerStat.GetBaseDamage(),
            StatType.CritPower => playerStat.GetCritPower(),
            StatType.CritChance => playerStat.GetCritChance(),
            StatType.FireDamage => playerStat.offense.fireDamage.GetValue(),
            StatType.IceDamage => playerStat.offense.iceDamage.GetValue(),
            StatType.LightningDamage => playerStat.offense.lightningDamage.GetValue(),
            StatType.ArmorReduction => playerStat.GetArmorReduction() * 100,
            StatType.AttackSpeed => playerStat.offense.attackSpeed.GetValue() * 100,
            StatType.ElementalDamage => playerStat.GetElementalDamage(out ElementType elementType, 1),

            //Resource Stat
            StatType.MaxHealth => playerStat.GetMaxHealth(),
            StatType.HealthRegen => playerStat.resources.healthRegen.GetValue(),

            _ => 0f
        };
    }
    private bool IspercentageStat(StatType type)
    {
        return type switch
        {
            StatType.CritChance or
            StatType.CritPower or
            StatType.ArmorReduction or
            StatType.IceResistance or
            StatType.FireResistance or
            StatType.LightningResistance or
            StatType.Evasion or
            StatType.AttackSpeed => true,
            
            _ => false,
        };
    }

    
}
